<template>
	<div style="">
		<el-dialog title="结账" :visible.sync="payVisible" :close-on-click-modal="false" width="70%" center append-to-body>
			<el-row :gutter="10">
				<el-col :span="10">
					<el-card shadow="never" style="height:430px;overflow: auto;">
						<el-table :data="orderInfo.foodVos" style="width: 100%;">
							<el-table-column prop="foodName" label="品名">
								<template slot-scope="scope">
									<div>{{ scope.row.foodName }}</div>
									<div style="color: forestgreen;font-size: 0.5rem;">{{ scope.row.requirementName }}</div>
								</template>
							</el-table-column>
							<el-table-column prop="foodNum" label="数量"></el-table-column>
							<el-table-column prop="price" label="金额">
								<template slot-scope="scope">
									<div style="color: red;">
										<span v-show="scope.row.discountSign !== 1">￥{{ Number(scope.row.total) }}元</span>
										<span v-show="scope.row.discountSign === 1" style="text-decoration: line-through;">￥{{ Number(scope.row.total) }}元</span>
									</div>
									<div style="color: green;" v-show="scope.row.discountSign === 1">￥{{ Number(scope.row.discountMoney) }}元</div>
								</template>
							</el-table-column>
						</el-table>
					</el-card>
					<br />
					<el-card shadow="never">
            <el-row :gutter="20">
              <el-col :span="12">
                <div style="text-align: center;">
                  <div>
                    菜品总价：
                    <span style="color: red;">￥{{ orderInfo.totalMoney }}元</span>
                  </div>
                  <div>
                    折后价：
                    <span style="color: red;">￥{{ orderInfo.discountMoney }}元</span>
                  </div>
                  <div v-if="memberId !== null">（会员：-&nbsp;&nbsp;{{ memberDiscountMoney }})</div>
                  <div v-if="discountName !== ''">（折扣方案：-&nbsp;&nbsp;{{ discountMethodMoney }})</div>
                  <div>
                    抹零：
                    <span style="color: red;">￥{{ wipeZero }}元</span>
                  </div>
                </div>
              </el-col>
              <el-col :span="12">
                <div style="text-align: center;">
                  <div>
                    整单折扣金额：
                    <span style="color: red;">￥{{ changeDiscountMoney }}元</span>
                  </div>
                  <div>
                    整单减免金额：
                    <span style="color: red;">￥{{ changeReduceMoney }}元</span>
                  </div>
                </div>
              </el-col>
            </el-row>
					</el-card>
				</el-col>
				<el-col :span="14">
					<el-form ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
						<el-card shadow="never">
							<el-row :gutter="20">
								<el-col :span="16" style="font-size: 1.25rem;">
									应收金额：
									<span style="color: red;">￥{{ totalMoney }}元&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
								</el-col>
								<el-col :span="8" style="font-size: 1.25rem;">
									折扣：
									<span style="color: red;">{{ discount }}折</span>
								</el-col>
							</el-row>
							<br />
							<el-row :gutter="20">
								<el-col :span="16" style="font-size: 1.25rem;">
									已收金额：
									<span style="color: red;">￥{{ alreadyGet }}元&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
								</el-col>
								<el-col :span="8" style="font-size: 1.25rem;">
									找零：
									<span style="color: red;">￥{{ zero }}元</span>
								</el-col>
							</el-row>
							<br />
							<el-row :gutter="20">
								<el-col :span="16" style="font-size: 1.25rem;">
									待收金额：
									<span style="color: red;">￥{{ stayGet }}元&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
								</el-col>
							</el-row>
							<br />
							<el-row :gutter="10">
								<el-col :span="4" v-for="items in payMethodList" :key="items.id">
									<a v-show="items.sysBuiltIn === 1" style="cursor: pointer;text-decoration: none;" @click="selectPayMethod(items)">
										<div class="seatNum" style="width: 100%;height: 60px;background-color:darkgray;padding-top: 18px;">
											<i v-if="items.paySelectId === 1" class="el-icon-circle-check"></i>
											{{ items.payMethodName }}
											<span v-if="items.payMoney !== '0.00'" style="color: white;">{{ items.payMoney }}</span>
										</div>
									</a>
								</el-col>
								<el-col :span="4">
									<a style="cursor: pointer;text-decoration: none;">
										<el-popover placement="right" width="700px" trigger="click">
											<el-col :span="24" v-for="items in payMethodList" :key="items.id">
												<a v-show="items.sysBuiltIn === 2" style="cursor: pointer;text-decoration: none;" @click="selectPayMethod(items)">
													<div class="seatNum" style="width: 100%;height: 60px;background-color:darkgray;padding-top: 18px;">
														<i v-if="items.paySelectId === 1" class="el-icon-circle-check"></i>
														{{ items.payMethodName }}
														<span v-if="items.payMoney !== '0.00'" style="color: white;">{{ items.payMoney }}</span>
													</div>
													<br />
												</a>
											</el-col>
											<div slot="reference" class="seatNum" style="width: 100%;height: 60px;background-color:yellowgreen;padding-top: 18px;">更多</div>
										</el-popover>
									</a>
								</el-col>
							</el-row>
							<br />
							<el-row :gutter="30">
								<el-col :span="10"><KeyBoardPoint @getPayNum="getPayInput" ref="KeyBoardPointRef"></KeyBoardPoint></el-col>
								<el-col :span="14">
									<el-row :gutter="20">
                    <el-col :span="8">
                      <a style="cursor: pointer;text-decoration: none;" @click="changePrice">
                        <el-popover placement="right" width="500px" trigger="click" v-model="changePriceVisle">
                          <ChangePriceKeyBoard @getDiscountMoney="changePr"
                                               ref="ChangePriceKeyBoardRef"></ChangePriceKeyBoard>
                          <div slot="reference" class="seatNum"
                               style="width: 100%;height: 50px;background-color:yellowgreen;padding-top: 12px;">
                            整单改价
                          </div>
                        </el-popover>
                      </a>
                    </el-col>
                    <el-col :span="8">
                      <a style="cursor: pointer;text-decoration: none;" @click="changeDiscount">
                        <el-popover placement="right" width="500px" trigger="click" v-model="changeDiscountVisle">
                          <ChangeDiscountKeyBoard
                            @getDiscountMoney="changeDis"
                            ref="ChangeDiscountKeyBoardRef"
                          ></ChangeDiscountKeyBoard>
                          <div slot="reference" class="seatNum"
                               style="width: 100%;height: 50px;background-color:palevioletred;padding-top: 12px;">
                            整单折扣
                          </div>
                        </el-popover>
                      </a>
                    </el-col>
										<el-col :span="8">
											<a style="cursor: pointer;text-decoration: none;" @click="miandan">
												<div class="seatNum" style="width: 100%;height: 50px;background-color:lightsteelblue;padding-top: 12px;">免单</div>
											</a>
										</el-col>
									</el-row>
									<br />
									<el-row :gutter="20">
										<el-col :span="8">
											<a style="cursor: pointer;text-decoration: none;" @click="discountDetailList">
												<div class="seatNum" style="width: 100%;height: 50px;background-color:deepskyblue;padding-top: 12px;">折扣方案</div>
											</a>
										</el-col>
										<el-col :span="8">
											<a style="cursor: pointer;text-decoration: none;" @click="memberList">
												<div class="seatNum" style="width: 100%;height: 50px;background-color:gray;padding-top: 12px;">会员</div>
											</a>
										</el-col>
										<el-col :span="8">
											<a style="cursor: pointer;text-decoration: none;" @click="accountUnitList">
												<div class="seatNum" style="width: 100%;height: 50px;background-color:thistle;padding-top: 12px;">挂单</div>
											</a>
										</el-col>
									</el-row>
									<br />
									<el-row>
										<el-col :span="24">
											<div style="border: 1px solid wheat;">
												<el-row style="padding-top: 1.25rem;padding-left: 1.25rem;">
													<el-col :span="12">
														名字：
														<span style="color: green;">{{ memberName }}</span>
													</el-col>
													<el-col :span="12">
														类型：
														<span style="color: green;">{{ memberTypeName }}</span>
													</el-col>
												</el-row>
												<el-row style="padding-top: 1.25rem;padding-left: 1.25rem;">
													<el-col :span="12">
														余额：
														<span style="color: red;">{{ option.data[0].list[1].value }}</span>
													</el-col>
													<el-col :span="12">
														消费：
														<span style="color: red;">{{ option.data[0].list[2].value }}</span>
													</el-col>
												</el-row>
												<el-row style="padding-top: 1.25rem;padding-left: 1.25rem;">
													<el-col :span="12">积分：{{ option.data[0].list[0].value }}</el-col>
													<el-col :span="12">折扣：{{ orderInfo.discount }}折</el-col>
												</el-row>
                        <el-row style="padding-top: 0.625rem;padding-left: 1.25rem;">
													<el-col :span="12"><el-button size="mini" @click="clearMemberInfo" type="infor">清除</el-button></el-col>
												</el-row>
												<br />
											</div>
										</el-col>
									</el-row>
								</el-col>
							</el-row>
						</el-card>
					</el-form>
				</el-col>
			</el-row>
		</el-dialog>

		<!-- 会员列表弹出层 -->
		<el-dialog center title="会员列表" :visible.sync="memberListVisile" width="70%" append-to-body><MemberList @getSelectMemberData="memberInfo"></MemberList></el-dialog>

		<el-dialog center title="会员列表" :visible.sync="chuMemberListVisile" width="70%" append-to-body>
			<ChuMemberList @getSelectMemberData="chuMemberInfo"></ChuMemberList>
		</el-dialog>

		<!-- 挂账单位列表弹出层 -->
		<el-dialog center title="挂账单位列表" :visible.sync="AccountUnitListVisible" width="70%" append-to-body>
			<AccountUnitList @getSelectAccountUnitData="accountUnitInfo"></AccountUnitList>
		</el-dialog>

		<!-- 折扣方案 -->
		<el-dialog center title="折扣方案列表" :visible.sync="DisCountListVisle" width="40%" append-to-body>
			<DisCountList @DiscountDetail="getDiscountDetailList" ref="DisCountListRef"></DisCountList>
		</el-dialog>

		<!-- 支付打印 -->
		<WMPayPrint @DataIsNull="toPan" v-show="false" ref="WMPayPrintRef"></WMPayPrint>
	</div>
</template>

<script>
//储值卡扣款用
import ChuMemberList from '../../modules/store/member/recharge/memberList.vue'
import MemberList from '../../modules/store/member/recharge/memberList.vue';
import AccountUnitList from '../component/AccountUnitList.vue';

import WMPayPrint from '../../modules/common/printTemplate/WaiMai/WMPayPrint.vue';
import DisCountList from '../component/DisCountList.vue';
import KeyBoardPoint from '../component/PayKeyboard.vue';
import ManualDiscountKeyBord from '../component/ManualDiscountKeyBord.vue';
import ChangePriceKeyBoard from '../component/ChangePriceKeyBoard.vue';
import ChangeDiscountKeyBoard from '../component/ChangeDiscountKeyBorad.vue';
import {keepTwoPoint} from "../../../utils";
export default {
	components: {
		MemberList,
		AccountUnitList,
		WMPayPrint,
		DisCountList,
		ManualDiscountKeyBord,
		ChangePriceKeyBoard,
		ChangeDiscountKeyBoard,
		KeyBoardPoint,
		ChuMemberList
	},

	data() {
		return {
      changeDiscountMoney: 0,
      changeReduceMoney: 0,
			clearDesk: false,
			numData: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, '.'],
			numArray: [],
			totalNum: '',
			payVisible: false,
			payMethodList: [],
			orderInfo: {
				totalMoney: 0
			},
			payMethodName: '现金',
			discount: 1,
			reallyGet: 0,
			remark: '',
			memberListVisile: false,
			AccountUnitListVisible: false,
			option: {
				span: 24,
				data: [
					{
						click: function(item) {
							alert(JSON.stringify(item));
						},
						title: '',
						subtitle: '',
						img: 'https://avatar.gitee.com/uploads/61/632261_smallweigit.jpg!avatar100?1518660401',
						color: '#00a7d0',
						list: [
							{
								label: '积分',
								value: ''
							},
							{
								label: '余额',
								value: ''
							},
							{
								label: '消费总额',
								value: ''
							},
							{
								label: '折扣',
								value: ''
							}
						]
					}
				]
			},
			memberName: '',
			unitInfo: {},
			accountId: null,
			memberId: null,
			memberInfos: {},
			memberTypeName: null,
			memberAccountInfo: {},
			DisCountListVisle: false,
			discountName: '',
			inputSig: '',
			deskName: '',
			zero: 0.0,
			morePayVisle: false,
			paySelectList: [],
			totalMoney: 0,
			keyboardInputNumber: '', //键盘输入
			initNumber: 0,
			suanMoney: '',
			payItem: '',
			wipeZero: '',
			discountMethodMoney: 0,
			memberDiscountMoney: 0,

			total:0,
			alreadyGet: 0,
			stayGet: 0,
			clickPayIndex: 0,
			changeSign: '',
			changePriceVisle: false,
			changeDiscountVisle: false,
			chuMemberListVisile: false,
			storeCard: '',
			storeCardPayInfo: '',
			memberBalance: '',
			changeMoneySign: '',
			payStr:'',
      changePriceSign: 1,
      changeDiscountSign: 1,
      bakChangeDisMoney: 0,
      bakChangePriceMoney: 0,
		};
	},

	created() {

	},

	methods: {
		changePr(money) {
      if (money > this.orderInfo.totalMoney) {
        this.$message({
          message: '改价金额不能大于应收',
          type: 'info',
          duration: 1500
        });
      } else {
        this.changeReduceMoney = 0
        if (this.changePriceSign === 1) {
          this.changePriceSign++
          this.bakChangePriceMoney = this.totalMoney
          this.changeReduceMoney = this.totalMoney - money
          this.totalMoney = money;
          this.stayGet = money;
        } else {
          this.changeReduceMoney = this.bakChangePriceMoney - money
          this.totalMoney = money;
          this.stayGet = money;
        }
        this.changePriceVisle = false
        this.discount = '';
        this.zero = 0;
        this.total = 0;
        this.alreadyGet = 0;
        this.payMethod();
      }
		},

		changePrice() {
			this.$refs.ChangePriceKeyBoardRef.init('改价为');
		},

		changeDis(discount) {
      if (discount < 1 || discount > 10) {
        this.$message({
          message: '请输入合法折扣数',
          type: 'info',
          duration: 1500
        });
      } else {
        this.changeDiscountMoney = 0
        if (this.changeDiscountSign === 1) {
          this.bakChangeDisMoney = this.totalMoney
          this.changeDiscountMoney = this.$wipeZeroRule(this.totalMoney - (discount / 10) * this.totalMoney)
          this.totalMoney = this.$wipeZeroRule((discount / 10) * this.totalMoney)
          this.changeDiscountSign++
          this.stayGet = this.totalMoney
        } else {
          this.changeDiscountMoney = this.$wipeZeroRule(this.bakChangeDisMoney - (discount / 10) * this.bakChangeDisMoney)
          this.totalMoney = this.$wipeZeroRule((discount / 10) * this.bakChangeDisMoney)
          this.stayGet = this.totalMoney
        }
        this.changeDiscountVisle = false
        this.zero = 0;
        this.total = 0;
        this.alreadyGet = 0;
        this.discount = discount;
        this.payMethod();
      }
		},

		changeDiscount() {
			this.$refs.ChangeDiscountKeyBoardRef.init('折扣');
		},

		//清除会员信息
		clearMemberInfo() {
      this.memberId = null;
      this.memberName = '';
      this.memberTypeName = '';
      this.option.data[0].list[0].value = '';
      this.option.data[0].list[1].value = '';
      this.option.data[0].list[2].value = '';
      this.orderInfo.memberAccountInfo = {};
      this.memberAccountInfo = {};
      this.orderInfo.memberInfos = {};
      this.orderInfo.discount = '';
      this.stayGet = this.orderInfo.totalMoney;
      this.alreadyGet = 0;
      this.totalMoney = this.orderInfo.totalMoney;
      if (this.discountName === '') {
        this.orderInfo.discountMoney = this.orderInfo.totalMoney
        this.wipeZero = 0
      } else {
        this.orderInfo.discountMoney = this.orderInfo.totalMoney - this.discountMethodMoney
        let wipe = this.$wipeZeroRule(this.orderInfo.discountMoney)
        this.wipeZero = Number(wipe) - Number(this.orderInfo.discountMoney)
        this.orderInfo.discountMoney = wipe
        this.totalMoney = wipe
        this.stayGet = wipe
      }
      this.discount = '';
      this.total = 0;
      //清除储值卡支付信息
      var arr = [];
      this.payMethodList.forEach(item => {
        if (item.payMethodName === '储值卡') {
          item.payMoney = undefined;
          this.storeCard = '';
          this.storeCardPayInfo = '';
        }
        arr.push(item);
      });
      this.payMethodList = arr;
		},

		resetPay() {
			this.paySelectList = [];
		},

		//获取优惠金额
		DisCountMoney(money) {
			this.reallyGet = this.totalMoney - money;
			this.$nextTick(() => {
				this.$refs.KeyBoardPointRef.init(this.reallyGet, '现金');
			});
		},

		//手动改价
		getChangeMoney(money) {
			if (this.paySelectList.length > 1 && money > this.reallyGet) {
				this.$message({
					message: '组合支付单项金额不能大于实收金额',
					type: 'info',
					duration: 1500
				});
			} else {
				var arr = [];
				this.paySelectList.forEach(item => {
					if (item.payMethodId === this.payItem.payMethodId) {
						item.payMoney = money;
					} else {
						item.payMoney = this.reallyGet - money;
					}
					arr.push(item);
				});
				if (money > this.reallyGet) {
					this.zero = money - this.reallyGet;
				} else {
					this.zero = 0;
				}
				this.paySelectList = arr;
			}
		},

		toChange(item) {
			this.payItem = item;
			this.$refs.ChangeKeyBordREf.init('金额');
		},

		toDiscount() {
			this.$refs.ManualDiscountKeyBordREf.init('优惠金额');
		},

		getDiscountDetailList(row) {
      this.discountMethodMoney = 0;
      this.discountName = row.info.discountName;
      var discountList = row.list;
      var arr = [];
      this.orderInfo.foodVos.forEach(item => {
        discountList.forEach(item2 => {
          if (item.id === item2.foodId) {
            item.discountMoney = ((item.total * item2.discount) / 10);
            item.discountSign = 1;
            var mon = item.total - item.discountMoney;
            this.discountMethodMoney += mon;
          }
        });
        arr.push(item);
      });
      this.orderInfo.foodVos = arr;
      if (this.memberId !== null) {
        this.orderInfo.discountMoney -= this.discountMethodMoney;
      } else {
        this.orderInfo.discountMoney = 0;
        arr.forEach(item => {
          if (item.discountSign === 1) {
            this.orderInfo.discountMoney += item.discountMoney;
          } else {
            this.orderInfo.discountMoney += item.total;
          }
        });
      }
      //取整抹零
      //取整抹零
      const wipe = this.$wipeZeroRule(this.orderInfo.discountMoney)
      this.wipeZero = keepTwoPoint(wipe - this.orderInfo.discountMoney)
      this.orderInfo.discountMoney = this.$wipeZeroRule(this.orderInfo.discountMoney)
      this.stayGet = this.orderInfo.discountMoney;
      this.totalMoney = this.orderInfo.discountMoney;
      this.changeDiscountMoney = 0
      this.changeReduceMoney = 0
      this.payMethod();
      this.DisCountListVisle = false;
		},

		morePay() {
			this.morePayVisle = true;
		},

		inputSign() {
			this.inputSig = 1;
		},


		//折扣方案
		discountDetailList() {
			this.DisCountListVisle = true;
			this.$nextTick(() => {
				this.$refs.DisCountListRef.getDataList();
			});
		},

		toPan() {
			this.print();
		},

		//打印账单小票
		print() {
			this.orderInfo.eatType = '外卖';
			this.$nextTick(() => {
				this.$refs.WMPayPrintRef.init(this.orderInfo);
			});
		},

		//结算
		submitJiesuan() {
      this.orderInfo.reallyGet = this.alreadyGet;
      this.orderInfo.zero = this.zero;
      this.orderInfo.wipeZero = this.wipeZero;
				if (this.deskName !== '') {
					this.orderInfo.remark = '合并单【' + this.deskName + '】';
				} else {
					this.orderInfo.remark = this.remark;
				}
				//合并支付
				var map = []
				this.payMethodList.forEach(item => {
					if(item.payMoney !== undefined){
						this.payStr += item.payMethodName + '[' + item.payMoney + '],';
						const info = {
							payMethodName:item.payMethodName,
							payMoney:item.payMoney
						}
						map.push(info)
					}
				})
				this.orderInfo.payVo = map
				this.orderInfo.payMethodName = this.payStr
				this.orderInfo.accountId = this.accountId;
				this.orderInfo.memberId = this.memberId;
				this.orderInfo.memberInfos = this.memberInfos;
				this.orderInfo.unitInfo = this.unitInfo;
				this.orderInfo.memberAccountInfo = this.memberAccountInfo;
				this.orderInfo.memberTypeName = this.memberTypeName;
				this.$http({
					url: this.$http.adornUrl(`/reception/tstoretakeoutrecord/save`),
					method: 'post',
					data: this.$http.adornData({
						takeoutInfo: this.orderInfo
					})
				}).then(({ data }) => {
					if (data && data.code === 0) {
						this.$message({
							message: '结账成功',
							type: 'success',
							duration: 1500
						});
						this.orderInfo.takeoutId = data.foodInfo.takeoutId
						this.orderInfo.time = data.foodInfo.time
						this.orderInfo.address = data.foodInfo.address
						this.orderInfo.staffName = data.foodInfo.staffName
						this.print();
					} else {
						this.$message({
							message: data.msg,
							type: 'danger',
							duration: 1500
						});
					}
					this.$emit('refreshDataList');
					this.memberId = null;
					this.accountId = null;
					this.memberName = '';
					this.memberTypeName = '';
					this.option.data[0].list[0].value = '';
					this.option.data[0].list[1].value = '';
					this.option.data[0].list[2].value = '';
					this.discount = 1;
					this.unitInfo = {};
					this.reallyGet = 0;
					this.payVisible = false;
				});
		},

    getPayInput(payInfo) {
      var changeArr = [];
      this.payMethodList.forEach(changeItem => {
        if (payInfo.payname === changeItem.payMethodName && payInfo.bakMoney === changeItem.payMoney) {
          changeItem.payMoney = payInfo.paymoney;
          //更新实收(bakMoney为原始数据，paymoney为更改数据)
          let change1 = payInfo.bakMoney - payInfo.paymoney;
          //更改支付方式的金额小于原始金额
          if (change1 > 0) {
            this.alreadyGet -= Math.abs(change1);
            //倘若已收金额小于总金额，则需要改变待收金额，若高于，则待收金额为0
            if (this.alreadyGet < this.totalMoney) {
              this.stayGet += this.totalMoney - this.alreadyGet;
              //找零
              this.zero = 0;
              this.total -= Math.abs(change1);
            } else {
              this.stayGet = 0;
              this.zero = this.alreadyGet - this.totalMoney;
              this.total -= Math.abs(change1);
            }
          }
          // 大于原始金额
          else {
            this.alreadyGet += Math.abs(change1);
            this.stayGet = 0;
            this.zero += this.alreadyGet - this.totalMoney;
            this.total += Math.abs(change1);
          }
        }
        changeArr.push(changeItem);
      });
      this.payMethodList = changeArr;

      this.alreadyGet += payInfo.paymoney
      this.zero = this.alreadyGet - this.totalMoney
      let ihArray = []
      this.payMethodList.forEach(ih => {
        if (ih.payMethodName === payInfo.payname) {
          ih.payMoney = payInfo.paymoney
        }
        ihArray.push(ih)
      })
      this.payMethodList = ihArray

      if (this.alreadyGet < this.totalMoney) {
        var arr = [];
        this.payMethodList.forEach(item => {
          if (payInfo.payname === item.payMethodName) {
            if (item.repeatSubmitSign === undefined) {
              //储值卡支付，判断会员余额是否够支付
              if (payInfo.payname === '储值卡') {
                if (this.memberBalance < payInfo.paymoney) {
                  this.$message({
                    dangerouslyUseHTMLString: true,
                    message: '<strong style="color:red;">储值卡金额为[' + this.memberBalance + '],不够支付当前订单金额，已重置支付</strong>',
                    type: 'success',
                  });
                  // this.memberId = null
                  this.$nextTick(() => {
                    this.$refs.KeyBoardPointRef.init(this.totalMoney, '现金');
                  });
                  let arr1 = []
                  this.payMethodList.forEach(item => {
                    if (item.payMethodName === '现金') {
                      item.paySelectId = 1
                    } else {
                      item.paySelectId = 2
                    }
                    item.payMoney = undefined
                    item.repeatSubmitSign = undefined
                    arr1.push(item)
                  })
                  this.alreadyGet = 0
                  this.zero = 0
                  this.stayGet = this.totalMoney
                  this.total = 0
                } else {
                  item.payMoney = payInfo.paymoney;
                  this.total += payInfo.paymoney;
                  // //已收
                  this.alreadyGet = this.total;
                  //待收
                  this.stayGet -= payInfo.paymoney;
                  //重复提交标识
                  item.repeatSubmitSign = 1;
                }
              } else {
                //找零(输入金额大于单项支付最高金额)
                if (payInfo.paymoney > payInfo.bakMoney) {
                  this.zero += payInfo.paymoney - payInfo.bakMoney;
                  item.payMoney = payInfo.paymoney;
                  //待收
                  if (this.zero > 0) {
                    this.stayGet = 0;
                  } else {
                    this.stayGet -= payInfo.paymoney;
                  }
                  //total为所有支付方式总共金额
                  this.total += payInfo.paymoney;
                  //已收
                  this.alreadyGet = this.total;
                  item.repeatSubmitSign = 1;
                  this.total = 0
                } else {
                  item.payMoney = payInfo.paymoney;
                  //total为所有支付方式总共金额
                  this.total += payInfo.paymoney;
                  //已收
                  this.alreadyGet = this.total;
                  //待收
                  this.stayGet -= payInfo.paymoney;
                  this.zero = 0
                  item.repeatSubmitSign = 1;
                }
              }
            }
          }
          arr.push(item);
        });
        this.payMethodList = arr;
      } else {
        this.stayGet = 0
        if (payInfo.payname === '储值卡') {
          if (this.memberBalance < payInfo.paymoney) {
            this.$message({
              dangerouslyUseHTMLString: true,
              message: '<strong style="color:red;">储值卡金额为[' + this.memberBalance + '],不够支付当前订单金额，已重置支付</strong>',
              type: 'success',
            });
            this.$nextTick(() => {
              this.$refs.KeyBoardPointRef.init(this.totalMoney, '现金');
            });
            let arr1 = []
            this.payMethodList.forEach(item => {
              if (item.payMethodName === '现金') {
                item.paySelectId = 1
              } else {
                item.paySelectId = 2
              }
              item.payMoney = undefined
              item.repeatSubmitSign = undefined
              arr1.push(item)
            })
            this.alreadyGet = 0
            this.zero = 0
            this.stayGet = this.totalMoney
            this.total = 0
          } else {
            this.$confirm('确定进行结账操作吗, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            })
              .then(() => {
                this.submitJiesuan();
              })
              .catch(() => {
                let arrs = []
                this.payMethodList.forEach(item => {
                  if (item.payMethodName === '现金') {
                    item.paySelectId = 1
                  } else {
                    item.paySelectId = 2
                  }
                  item.payMoney = undefined
                  item.repeatSubmitSign = undefined
                  arrs.push(item)
                })
                this.alreadyGet = 0
                this.zero = 0
                this.stayGet = this.totalMoney
                this.total = 0
                this.payMethodList = arrs
                this.$nextTick(() => {
                  this.$refs.KeyBoardPointRef.init(this.totalMoney, '现金');
                });
                this.$message({
                  type: 'info',
                  message: '已重置支付，请重新选择'
                });
              });
          }
        } else {
          this.$confirm('确定进行结账操作吗, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
            .then(() => {
              this.submitJiesuan();
            })
            .catch(() => {
              let arrs = []
              this.payMethodList.forEach(item => {
                if (item.payMethodName === '现金') {
                  item.paySelectId = 1
                } else {
                  item.paySelectId = 2
                }
                item.payMoney = undefined
                item.repeatSubmitSign = undefined
                arrs.push(item)
              })
              this.alreadyGet = 0
              this.zero = 0
              this.stayGet = this.totalMoney
              this.total = 0
              this.payMethodList = arrs
              this.$nextTick(() => {
                this.$refs.KeyBoardPointRef.init(this.totalMoney, '现金');
              });
              this.$message({
                type: 'info',
                message: '已重置支付，请重新选择'
              });
            });
        }
      }
    },

		selectPayMethod(item) {
			this.clickPayIndex++;
			const arr = [];
			this.payMethodList.forEach((it, index) => {
				if (index === 0 && it.sysBuiltIn === 1 && this.clickPayIndex === 1) {
					if (it.payMethodId === item.payMethodId) {
						it.paySelectId = 1;
						if (it.payMoney === undefined) {
							this.$nextTick(() => {
								this.$refs.KeyBoardPointRef.init(this.totalMoney - this.alreadyGet, it.payMethodName);
							});
						} else {
							this.$nextTick(() => {
								this.$refs.KeyBoardPointRef.init(it.payMoney, it.payMethodName);
							});
						}
					} else {
						it.paySelectId = 2;
					}
					arr.push(it);
					this.payMethodList = arr;
					this.clickPayIndex--;
				} else {
					if (it.payMethodId === item.payMethodId) {
						it.paySelectId = 1;
						//储值卡
						if (it.payMethodName === '储值卡') {
							//如果没有选择会员
							if (this.memberId === null) {
								this.chuMemberListVisile = true;
							} else {
								if (it.payMoney === undefined) {
									this.$nextTick(() => {
										this.$refs.KeyBoardPointRef.init(this.totalMoney - this.alreadyGet, it.payMethodName);
									});
								} else {
									this.$nextTick(() => {
										this.$refs.KeyBoardPointRef.init(it.payMoney, it.payMethodName);
									});
								}
							}
						} else {
							//还没有设置支付金额
							if (it.payMoney === undefined) {
								if (this.alreadyGet == 0) {
									this.$nextTick(() => {
										this.$refs.KeyBoardPointRef.init(this.totalMoney, it.payMethodName);
									});
								} else {
									//已收大于待收
									if (this.alreadyGet > this.totalMoney) {
										this.$nextTick(() => {
											this.$refs.KeyBoardPointRef.init(0, it.payMethodName);
										});
									} else {
										this.$nextTick(() => {
											this.$refs.KeyBoardPointRef.init(this.totalMoney - this.alreadyGet, it.payMethodName);
										});
									}
								}
							} else {
								this.$nextTick(() => {
									this.$refs.KeyBoardPointRef.init(it.payMoney, it.payMethodName);
								});
							}
						}
					} else {
						it.paySelectId = 2;
					}
					arr.push(it);
					this.payMethodList = arr;
				}
			});

			this.payMethodInfo = item;
			this.payMethodName = item.payMethodName;
			this.orderInfo.payMethodName = item.payMethodName;
		},

		//支付方式列表
		payMethod() {
			this.$http({
				url: this.$http.adornUrl('/store/tstorepaymethod/list'),
				method: 'get',
				params: this.$http.adornParams({
					page: this.pageIndex
				})
			}).then(({ data }) => {
				if (data && data.code === 0) {
					this.payMethodList = data.page.list;
					//支付默认选中
					const arr = [];
					this.payMethodList.forEach((it, index) => {
						if (index === 0) {
							it.paySelectId = 1;
							this.$nextTick(() => {
								this.$refs.KeyBoardPointRef.init(this.totalMoney, it.payMethodName);
							});
						}
						arr.push(it);
					});
					this.payMethodList = arr;
				} else {
					this.payMethodList = [];
				}
			});
		},

		//免单
		miandan() {
			this.$confirm('确定免单吗？', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			})
				.then(() => {
					this.orderInfo.reallyGet = 0;
          this.orderInfo.remark = '免单';
          this.orderInfo.payMethodName = '免单';
          this.orderInfo.accountId = this.accountId;
					this.orderInfo.accountId = this.accountId;
					this.orderInfo.memberId = this.memberId;
					this.orderInfo.memberInfos = this.memberInfos;
					this.orderInfo.unitInfo = this.unitInfo;
					this.orderInfo.memberAccountInfo = this.memberAccountInfo;
					this.orderInfo.memberTypeName = this.memberTypeName;
					this.$http({
						url: this.$http.adornUrl(`/reception/tstoretakeoutrecord/save`),
						method: 'post',
						data: this.$http.adornData({
							takeoutInfo: this.orderInfo
						})
					}).then(({ data }) => {
						if (data && data.code === 0) {
							this.$message({
								message: '免单成功',
								type: 'success',
								duration: 1500
							});
							this.print();
						} else {
							this.$message({
								message: data.msg,
								type: 'danger',
								duration: 1500
							});
						}
						this.$emit('refreshDataList');
						this.memberId = null;
						this.memberName = '';
						this.memberTypeName = '';
						this.option.data[0].list[0].value = '';
						this.option.data[0].list[1].value = '';
						this.option.data[0].list[2].value = '';
						this.discount = 1;
						this.unitInfo = {};
						this.reallyGet = 0;
						this.payVisible = false;
					});
				})
				.catch(() => {
					this.$message({
						type: 'info',
						message: '已取消免单'
					});
				});
		},

		//挂账单位
		accountUnitList() {
			if (this.memberId !== null) {
				this.$message({
					type: 'info',
					message: '已选择会员，不能进行挂单'
				});
			} else {
				this.AccountUnitListVisible = true;
			}
		},

		accountUnitInfo(row) {
			this.unitInfo = row;

			this.accountId = row.accountUnitId;

			this.AccountUnitListVisible = false;

			this.submitJiesuan();
		},

		//获取会员列表
		memberList() {
			this.memberListVisile = true;
		},

		//储值卡使用会员
		chuMemberInfo(row) {
      this.memberDiscountMoney = 0;
      this.changeDiscountMoney = 0
      this.changeReduceMoney = 0
      this.memberInfos = row;
      if (this.discountName !== '') {
        this.orderInfo.discountMoney = keepTwoPoint((row.defaultDiscount / 10) * this.totalMoney)
        this.memberDiscountMoney = keepTwoPoint((this.orderInfo.totalMoney - this.discountMethodMoney - this.orderInfo.discountMoney))
      } else {
        this.orderInfo.discountMoney = keepTwoPoint(((row.defaultDiscount / 10) * this.orderInfo.totalMoney))
        this.memberDiscountMoney = keepTwoPoint(this.orderInfo.totalMoney - this.orderInfo.discountMoney)
      }
      var wipe = this.$wipeZeroRule(this.orderInfo.discountMoney)
      this.wipeZero = keepTwoPoint((wipe - this.orderInfo.discountMoney))
      this.orderInfo.discountMoney = wipe
      this.alreadyGet = wipe
      this.stayGet = wipe
      this.totalMoney = wipe

      //如果已经使用了支付方式
      if (this.alreadyGet !== 0) {
        this.alreadyGet = 0;
        this.total = 0;
        const arr = [];
        this.payMethodList.forEach(item => {
          if (item.payMethodName === '储值卡') {
            this.$nextTick(() => {
              this.$refs.KeyBoardPointRef.init(this.totalMoney, row.payMethodName);
            });
          } else {
            item.payMoney = undefined;
          }
          arr.push(item);
        });
        this.payMethodList = arr;
      }

      this.discount = row.defaultDiscount;
      this.memberName = row.memberName;
      this.memberTypeName = row.memberTypeName;
      this.option.data[0].list[3].value = row.defaultDiscount;
      this.memberId = row.id;
      this.orderInfo.discount = this.discount;
      this.orderInfo.memberTypeId = row.memberTypeId;

      this.$http({
        url: this.$http.adornUrl(`/store/tstorememberaccountdetail/info/` + row.id),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.memberAccountInfo = data.tStoreMemberAccountDetail;
          this.option.data[0].list[0].value = data.tStoreMemberAccountDetail.integral;
          this.option.data[0].list[1].value = data.tStoreMemberAccountDetail.balace;
          this.option.data[0].list[2].value = data.tStoreMemberAccountDetail.totalConsume;
          this.memberBalance = data.tStoreMemberAccountDetail.balace;
          this.chuMemberListVisile = false;
          this.$message({
            message: '选择了会员【' + row.memberName + '】',
            type: 'success',
            duration: 1500
          });
          this.$nextTick(() => {
            this.$refs.KeyBoardPointRef.init(this.stayGet, '储值卡');
          });
        }
      });
		},

		memberInfo(row) {
      this.memberDiscountMoney = 0;
      this.changeDiscountMoney = 0
      this.changeReduceMoney = 0
      this.memberInfos = row;
      if (this.discountName !== '') {
        this.orderInfo.discountMoney = keepTwoPoint((row.defaultDiscount / 10) * this.totalMoney)
        this.memberDiscountMoney = keepTwoPoint((this.orderInfo.totalMoney - this.discountMethodMoney - this.orderInfo.discountMoney))
      } else {
        this.orderInfo.discountMoney = keepTwoPoint(((row.defaultDiscount / 10) * this.orderInfo.totalMoney))
        this.memberDiscountMoney = keepTwoPoint(this.orderInfo.totalMoney - this.orderInfo.discountMoney)
      }
      var wipe = this.$wipeZeroRule(this.orderInfo.discountMoney)
      this.wipeZero = keepTwoPoint((wipe - this.orderInfo.discountMoney))
      this.orderInfo.discountMoney = wipe
      this.alreadyGet = wipe
      this.stayGet = wipe
      this.totalMoney = wipe

      //如果已经使用了支付方式
      if (this.alreadyGet !== 0) {
        this.alreadyGet = 0;
        this.total = 0;
        this.zero = 0;
        this.payMethod();
      }

      this.discount = row.defaultDiscount;
      this.memberName = row.memberName;
      this.memberTypeName = row.memberTypeName;
      this.option.data[0].list[3].value = row.defaultDiscount;
      this.memberListVisile = false;
      this.memberId = row.id;
      this.orderInfo.discount = this.discount;
      this.orderInfo.memberTypeId = row.memberTypeId;

      //获取会员账户详情
      this.$http({
        url: this.$http.adornUrl(`/store/tstorememberaccountdetail/info/` + row.id),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.memberAccountInfo = data.tStoreMemberAccountDetail;
          this.option.data[0].list[0].value = data.tStoreMemberAccountDetail.integral;
          this.option.data[0].list[1].value = data.tStoreMemberAccountDetail.balace;
          this.option.data[0].list[2].value = data.tStoreMemberAccountDetail.totalConsume;
          this.memberBalance = data.tStoreMemberAccountDetail.balace;
        }
      });
		},

		init(params) {
			this.payVisible = true;

			this.payMethodList = [];
			this.zero = 0;
			this.wipeZero = 0;
			this.discountName = '';
			this.memberDiscountMoney = '';
			this.discountMethodMoney = '';
			this.memberId = null;
			this.orderInfo = params;
			this.totalMoney = 0;
			this.alreadyGet = 0;
			this.stayGet = 0;
			this.total = 0;
			this.orderInfo.discountMoney = 0;

			//清空会员信息
			this.memberAccountInfo = ''
			this.option.data[0].list[0].value =''
			this.option.data[0].list[1].value = ''
			this.option.data[0].list[2].value = ''
			this.memberBalance = ''
			this.memberName = ''
			this.memberTypeName = ''
			this.memberInfos = ''
			this.discount = ''

			this.orderInfo = params;
			this.totalMoney = params.totalMoney;
			this.reallyGet = params.totalMoney;

			this.payMethod();
			this.$nextTick(() => {
				this.$refs.KeyBoardPointRef.init(this.totalMoney,'现金')
			})
			if (params.sign === 1) {
				var foodVos = JSON.parse(params.foodVo);
				params.foodVos = foodVos;
				params.foodVo = foodVos.toString();
			}
		},

		//确认
		getInputNumber() {
			this.$emit('getEatNum', this.totalNum);
			this.payVisible = false;
		},

		getNumber(num) {
			this.numArray.push(num);
			this.totalNum += num.toString();
		},
		//清除数据
		clearNum() {
			this.numArray = [];
			this.totalNum = '';
		}
	}
};
</script>

<style>
/*透明渐变水平线*/
hr {
	width: 80%;
	margin: 0 auto;
	border: 0;
	height: 0;
	border-top: 1px solid rgba(0, 0, 0, 0.1);
	border-bottom: 1px solid rgba(255, 255, 255, 0.3);
}

.numClass {
	display: -moz-box; /*兼容Firefox*/
	display: -webkit-box; /*兼容FSafari、Chrome*/

	-moz-box-align: center; /*兼容Firefox*/
	-webkit-box-align: center; /*兼容FSafari、Chrome */

	-moz-box-pack: center; /*兼容Firefox*/
	-webkit-box-pack: center; /*兼容FSafari、Chrome */

	font-size: 25px;
	font-weight: 12000;

	background-color: white;
	color: #04b404;
	border-style: solid;
	border-width: 0.0625rem;
	border-color: #04b404;
}
</style>
